package com.jdk.demo.fileChannel.openfile;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Sync {

	// 要求对文件内容或元数据的每次更新都同步写入底层存储设备，如果这样做，程序运行的效率就降低了
	// dsync的作用：要求对文件内容每次更新都同步写入底层存储设备
	// 区别是：sync更新内容与元数据；dsync只更新内容
	public static void main(String[] args) throws Exception {
		File file = new File("q.txt");
		Path path = file.toPath();
		FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.SYNC, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
		long start = System.currentTimeMillis();
		for (int i = 0; i < 200; i++) {
			fileChannel.write(ByteBuffer.wrap("a".getBytes()));
		}
		long end = System.currentTimeMillis();
		System.out.println(end - start);
		fileChannel.close();
	}

}
